package feesmodule.converter;

import feesmodule.domain.ProgramToSericeMapInfo;
import feesmodule.domain.Service;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="service")
public class ServiceConverter 
{
    private Service service;

    public ServiceConverter()
    {
    }

    public ServiceConverter(Service service)
    {
        this.service = service;
    }

    @XmlElement(name="servicename")
    public String getServiceName()
    {
        return service.getServiceName();
    }

    @XmlElement(name="programs")
    public Collection<ProgramToServiceMapConverter> getPrograms()
    {
        List<ProgramToServiceMapConverter> mapping = new ArrayList<ProgramToServiceMapConverter>();
        ProgramToServiceMapConverter creditPrograms = null;
        ProgramToServiceMapConverter debitPrograms = null;
        ProgramToServiceMapConverter anyPrograms = null;
        List<ProgramToSericeMapInfo> programs = service.getProgramsMaping();
        for(ProgramToSericeMapInfo program: programs)
        {
            if(program.getDebitIndicator() == 1)
            {
                if(creditPrograms == null)
                    creditPrograms = new ProgramToServiceMapConverter(1);
                creditPrograms.convertProgram(program.getProgram());
            }
            else
                if(program.getDebitIndicator() == 2)
                {
                    if(debitPrograms == null)
                        debitPrograms = new ProgramToServiceMapConverter(2);
                   debitPrograms.convertProgram(program.getProgram());

                }
                else
                {
                    if(anyPrograms == null)
                         anyPrograms = new ProgramToServiceMapConverter(3);
                    anyPrograms.convertProgram(program.getProgram());
                }
        }
        if(creditPrograms != null)
            mapping.add(creditPrograms);
        if(debitPrograms != null)
            mapping.add(debitPrograms);
        if(anyPrograms != null)
            mapping.add(anyPrograms);
        return mapping;
    }

   
}
